package com.blb.service.impl;

import com.blb.entity.IoMove;
import com.blb.entity.Salary;
import com.blb.mapper.IoMoveMapper;
import com.blb.service.IIoMoveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存移库表 服务实现类
 * </p>
 *
 * @author xiaoxiong
 * @since 2020-05-20
 */
@Service
public class IoMoveServiceImpl extends ServiceImpl<IoMoveMapper, IoMove> implements IIoMoveService {

    @Resource
   private IoMoveMapper ioMoveMapper;

    @Override
    public List<IoMove> movePage(Integer draw, Integer start, Integer length) {
        //公司ID
        String company = ShiroUtils.getCompany();

        Map<String,Object> map=new HashMap<>();
        map.put("start",start);
        map.put("length",length);
        map.put("company",company);
        return ioMoveMapper.movePage(map);
    }

    @Override
    public int count() {
        String company = ShiroUtils.getCompany();
        return ioMoveMapper.count(company);
    }

    //移出商品的信息
    @Override
    public List<Map> outAll(String pname, String ck) {
        String company = ShiroUtils.getCompany();
        Map<String,Object> map=new HashMap<>();
        map.put("pname",pname);
        map.put("ck",ck);
        map.put("company",company);
        return ioMoveMapper.outAll(map);
    }

    @Override
    @Transactional
    public void moveAdd(Map map) {
        //公司ID
        String company = ShiroUtils.getCompany();

        //把当前货位上的商品清零
        String hid=(String) map.get("hid");//货位ID
         //调用清零商品的方法
            ioMoveMapper.delPname(hid);

        //把当前商品放到新的货位上面
            ioMoveMapper.moveSpaceUpdate(map);

        //记录移出商品的出库信息(调拨出库记录)
            //根据货位ID查询商品当前所署的仓库，用于出库记录
        String ckid = ioMoveMapper.byIdCk(hid);//出库的仓库ID
        Map<String,Object> outmap=new HashMap<>();
        String id = UUIDUtils.getId();
        String allotID=UUIDUtils.getId(); //调拨相关单号
        outmap.put("id",id);
        outmap.put("ioNo",id);
        outmap.put("ioType","调拨出库");
        outmap.put("batch",UUIDUtils.getId());
        outmap.put("pname",map.get("pname"));
        outmap.put("pNum","1");
        outmap.put("ck",ckid);
        outmap.put("allotID",allotID);
        outmap.put("company",company);
        ioMoveMapper.moveOutAdd(outmap);


        //记录移入信息(调拨入库记录)
        Map<String,Object> strMap=new HashMap<>();
        String id2 = UUIDUtils.getId();
        strMap.put("id",id2);
        strMap.put("ioNo",id2);
        strMap.put("ioType","调拨入库");
        strMap.put("batch",UUIDUtils.getId());
        strMap.put("pname",map.get("pname"));
        strMap.put("pNum","1");
        strMap.put("ck",map.get("ck"));
        strMap.put("allotID",allotID);
        strMap.put("company",company);
        ioMoveMapper.moveOutAdd(strMap);

        //记录库存移库的信息
       String id3= UUIDUtils.getId();
       map.put("id",id3);
       map.put("moveId",id3);
       map.put("outCk",ckid);
       map.put("pNum","1");
       map.put("company",company);
       ioMoveMapper.moveAdd(map);

    }
}
